package com.ccxe.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
/**
 * 基准日数据工具类
 * @author hetao
 *
 */
public class SampleDataUtil {

	public static final String SYSTEM_PATH = System.getProperty("user.dir");
	private static final File SAMPLE_DATA = new File(SYSTEM_PATH+File.separator+"conf/sample_data");
	public static void main(String[] args) {
		int[] i = getDatumDay(20050404, 15);
		for (int j : i) {
			System.out.println(j);
		}
	}
	
	/**
	 * 根据基准日获取样本,
	 * @param data 基准日,格式限定为 yyyyMMdd
	 * @param length 采取基准日前后长度
	 * @return 基准日内的日期
	 */
	public static int[] exportDatumDayTool(int data, int length) {
		Reader read = null;
		BufferedReader br = null;
		int array [] = null;
		try {
			read = new FileReader(SAMPLE_DATA); 
			br = new BufferedReader(read);
			List<Integer> list = new ArrayList<Integer>();
			while(br.ready()){
				int tmp = Integer.parseInt(br.readLine());
				list.add(tmp);
				System.out.print(tmp+",");
			}
			int flag = 0;
			for (int j = 0; j < list.size(); j++) {
				if(data == list.get(j)) {
					flag = j ;
					break;
				}
			}
			//记录开始的位置;
			int start = flag-length;
			//记录结束的位置;
			int end = flag+length;
			int count = 0;
			array = new int[(end - start)+1];
			for(int i = start; i <= end; i++) {
				array[count] = list.get(i);
				count ++;
			}
			return array;
		} catch (ArrayIndexOutOfBoundsException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		return new int[]{};
	}
	
	public static int[] getDatumDay(int data, int length) {
		int [] source = DATA.BEANCHMARK_DATA;
		int array [] = null;
		int flag = 0;
		for (int j = 0; j < source.length; j++) {
			if(data == source[j]) {
				flag = j ;
				break;
			}
		}
		//记录开始的位置;
		int start = flag-length;
		//记录结束的位置;
		int end = flag+length;
		int count = 0;
		array = new int[(end - start)+1];
		for(int i = start; i <= end; i++) {
			array[count] = source[i];
			count ++;
		}
		return array;
	}
}
